<?php
/**
 * 消息管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-08-05 
 */
namespace Home\Controller;

class ImController extends AllowController{
    /*
     * 构造函数
     * 
     * @return #
     */
    public function _initialize() {
        parent::_initialize();
        $this->db   =D("Im");
        $this->club =D('Club');
        $this->gclub=D('ClubGroup');
        $this->user =D('User');
    }
    
    /*
     * 系统消息列表
     * 
     * @return #
     */
    public function index(){
        
        $where=array(
            'accept_user_id'=>array(array((int)session('home.id')),array(0),'OR'),
            'status'        =>1,
            'type'          =>array(array(1),array(2),array(4),array(5),'OR')
        );
        
        $where1=array(
            'system_num'=>0,
            'id'        =>session("home.id")
        );
        $ord['id']  =desc;
        $this->user->save($where1);      
        //将session的数量也设为0
        $_SESSION['home']['system_num'] ='';
        $imList=$this->db->imList($where,$ord,15,1);
        $iList  =$imList['imList'];        
        //$enPage =enPage($imList['pageList']);
        $this->assign("pageList",$imList['pageList']);
        $this->assign("iList",$iList);
        $this->assign("act",1);
        $this->display();
    }
    
    /*
     * 删除提示消息
     * 
     * @return #
     */
    public function delIm(){
        $where=array(
            "id"            =>I("request.id",'',"code"),
            'status'        =>0
        );       
        $where['log_name']  =session("home.id");
        if(($this->db->del($where))){
            if(I("request.sign",'','code')){
                //$this->redirect("imList");
                $this->message();
            }else{
                $this->redirect("index");                
            }
        }else{
            if(I("request.sign",'','code')){
                $this->redirect("imList");
            }else{
                $this->redirect("index");                
            }
        }
    }
    
    /*
     * 我的留言信息列表
     * 
     * @return #
     */
    public function imList(){
        $where=array(
            'accept_user_id'=>session('home.id'),
            'status'        =>1,
            'type'          =>3
        );
        //将user表中的信息数量设为0
        $where1=array(
            'im_num'=>0,
            'id'    =>session("home.id")
        );
        $this->user->save($where1);      
        //将session的数量也设为0
        $_SESSION['home']['im_num'] ='';
        $ord['id']  =desc;
        $imList=$this->db->imList($where,$ord,15,1);
        $iList  =$imList['imList'];        
        //$enPage =enPage($imList['pageList']);
        $this->assign("pageList",$imList['pageList']);
        $this->assign("iList",$iList);
        $this->assign("act",2);
        $this->display("index");
    }
    
    /*
     * 同意申请加入活动或者俱乐部
     * 
     * @return #
     */
    public function agree(){
        $imInfo =$this->db->find(I("request.id",'',"code"));
        if($imInfo['is_gclub']){
            //说明对方申请的是俱乐部
            $where['id']=$imInfo['active_id'];
            //原俱乐部人员
            $info   =$this->gclub->where($where)->find();
            //判断是否为退出
            if($imInfo['is_out']){
                $where['user_ids']  =str_replace($imInfo['assign_user_id'].',','',$info['user_ids']);
                $cont   ="退出";
                $cont_  ="quit";
            }else{
                $where['user_ids']  =$info['user_ids'].$imInfo['assign_user_id'].',';
                $cont   ="加入";
                $cont_  ="join ";
            }   
            if($this->gclub->save($where)){
                $where1=array(
                    "id"    =>I("request.id",'',"code"),
                    'type'  =>5
                );
                $this->db->save($where1);
                //给申请人发送一条消息
                $data=array(
                    "accept_user_id"=>$imInfo['assign_user_id'],
                    "content"       =>"同意你".$cont . $info['name']."俱乐部",
                    "en_content"    =>"Agree with you to ".$cont_." the ".$info['en_name']." club",
                    'type'          =>4
                );
                $this->db->add($data);
                $this->user->where(array("id"=>$imInfo['assign_user_id']))->setInc("system_num");
                $this->redirect("index");
            }else{
                $this->redirect("index");
            }
        }else{
            //说明对方加入的是活动
            $where['id']    =$imInfo['active_id'];
            //原活动人员
            $info   =$this->club->where($where)->find();
            //判断是否为退出
            if($imInfo['is_out']){
                $where['user_ids']  =str_replace($imInfo['assign_user_id'].',','',$info['user_ids']);
                $cont   ="退出";
                $cont_  ="quit";
            }else{
                $where['user_ids']  =$info['user_ids'].$imInfo['assign_user_id'].',';
                $cont   ="加入";
                $cont_  ="join ";
            }
            $where['activity_ids']  =$info['assign_user_id'].$imInfo['assign_user_id'].',';
            if($this->club->save($where)){
            $where1=array(
                    "id"    =>$imInfo['assign_user_id'],
                    'type'  =>5
                );
                $this->db->save($where1);
                //给申请人发送一条消息
                $data=array(
                    "accept_user_id"=>I("request.assign_user_id"),
                    "content"       =>"同意你". $cont .$info['theme']."活动",
                    "en_content"    =>"Agree with you to ".$cont_." the ".$info['en_theme'],
                    'type'          =>4
                );
                $this->db->add($data);
                $this->user->where(array("id"=>$imInfo['assign_user_id']))->setInc("system_num");
                $this->redirect("index");
            }else{
                $this->redirect("index");
            }
        }
    }
    
    /*
     * 拒绝加入活动或者俱乐部
     * 
     * @return #
     */
    public function resufe(){
        $imInfo =$this->db->find(I("request.id",'',"code"));
        if($imInfo['is_gclub']){
            //说明对方申请的是俱乐部
            $where['id']=$imInfo['active_id'];
            //原俱乐部人员
            $info   =$this->gclub->where($where)->find();
            //给申请人发送一条拒绝消息
            if($imInfo['is_out']){
                $cont   ="退出";
                $cont_  ="quit";
            }else{
                $cont   ="加入";
                $cont_  ="join ";
            }
            $data=array(
                "accept_user_id"=>$imInfo['assign_user_id'],
                "content"       =>"拒绝你".$cont .$info['name']."俱乐部",
                "en_content"    =>"Refuse to ".$cont_ ." the ".$info['en_name']." club",
                'type'          =>4
            );
            $this->db->add($data);
            $this->user->where(array("id"=>$imInfo['assign_user_id']))->setInc("system_num");
            $this->redirect("index");
        }else{
            //说明对方加入的是活动
            $where['id']    =$imInfo['active_id'];
            //原活动人员
            $info   =$this->club->where($where)->find();
            //$where['activity_ids']  =$info['assign_user_id'].I("request.assign_user_id",'','code').',';
            //给申请人发送一条拒绝消息
            if($imInfo['is_out']){
                $cont   ="退出";
                $cont_  ="quit";
            }else{
                $cont   ="加入";
                $cont_  ="join ";
            }            
            $data=array(
                "accept_user_id"=>$imInfo['assign_user_id'],
                "content"       =>"拒绝你".$cont.$info['theme']."活动",
                "en_content"    =>"Refuse to ".$cont_." the ".$info['theme'],
                'type'          =>4
            );
            $this->db->add($data);
            $this->user->where(array("id"=>$imInfo['assign_user_id']))->setInc("system_num");
            $this->redirect("index");
        }
    }
    
    /*
     * 留言回复
     * 
     * @return #
     */
    public function reply(){
        $assign_user_id =$this->db->where(array("id"=>I("request.id")))->getField("assign_user_id");
        $where=array(
            'accept_user_id'=>$assign_user_id,
            'assign_user_id'=>$_SESSION['home']['id'],
            'content'       =>I("request.content"),
            'type'          =>3
        );
        if($this->db->add($where)){
            $this->user->where(array("id"=>$assign_user_id))->setInc("im_num");
            echo 1;
        }else{
            echo 2;
        }        
    }
    
    /*
     * 加好友处理
     * 
     * @return #
     */
    public function message(){
        if(!I("request.id","","code")){
            $this->error("参数错误");
        }
        $im =$this->db->find(I("request.id","","code"));
        if(I("request.sign",'','code')=="Y"){
            //同意，取出自己的好友列表，把对方的加进去
            $myFriendIds=$this->user->where(array("id"=>session("home.id")))->getField("friends_ids");
            $new_friends=$myFriendIds.$im['assign_user_id'].',';
            $where['id']            =session("home.id");
            $where['friends_ids']   =$new_friends;
            $this->user->save($where);
            //将自己的id加入对方好友列表
            $youFriends =$this->user->where(array("id"=>$im['assign_user_id']))->getField("friends_ids");
            $newYfriends=$youFriends.session("home.id").',';
            $where2['id']           =$im['assign_user_id'];
            $where2['friends_ids']  =$newYfriends;
            $this->user->save($where2);
            //删除friend数据
            D("Friends")->where(array("my_id"=>session("home.id"),'you_id'=>$im['assign_user_id']))->delete();
            
            //给对方发一条同意消息
            $where3=array(
                "assign_user_id"=>session("home.id"),
                "content"       =>session("home.name")."同意你的好友申请",
                "en_content"    =>session("home.name")." agrees with your friend.",
                "accept_user_id"=>$im['assign_user_id'],
                "add_time"      =>time(),
                "type"          =>4
            );
            $this->db->add($where3);
            $this->user->where(array("id"=>$im['assign_user_id']))->setInc("system_num");            
        }else{
            //拒绝加为好友
            $where4=array(
                "assign_user_id"=>session("home.id"),
                "content"       =>session("home.name")."拒绝你的好友申请",
                "en_content"    =>session("home.name")." refused to apply for your friend",
                "accept_user_id"=>$im['assign_user_id'],
                "add_time"      =>time(),
                "type"          =>4
            );
            $this->db->add($where4);
            $this->user->where(array("id"=>$im['assign_user_id']))->setInc("system_num");
        }
        //将本条消息状态更改
        $where5=array(
            "id"    =>I("request.id","","code"),
            "type"  =>5
        );
        $this->db->save($where5);
        $this->redirect("index"); 
    }
    
    /*
     * 发送留言
     * 
     * @return #
     */
    public function addMessage(){
        if(I("request.accept_user_id")){
            $map['accept_user_id']  =I("request.accept_user_id");
        }
        if(I("request.content")){
            $map['content']         =I("request.content");
        }
        $map['assign_user_id']      =session("home.id");
        $map['type']                =3;
        $mess   =$this->db->add($map);
        $sys_num=$this->user->where(array("id"=>I("request.accept_user_id")))->setInc("im_num");
        if($mess && $sys_num){
            echo 1;
        }else{
            echo 2;
        }
    }
    
}